home *** CD-ROM | disk | FTP | other *** search
- Path: news.ld.centuryinter.net!usenet
- From: steidl@centuryineter.net
- Newsgroups: comp.lang.c++
- Subject: Re: Run time dynamic 2-D array?
- Date: 14 Feb 1996 11:48:38 GMT
- Organization: Century Internet
- Message-ID: <4fsi6m$maf@news.ld.centuryinter.net>
- References: <4flcq9$i1k@vixen.cso.uiuc.edu>
- Reply-To: steidl@centuryineter.net
- NNTP-Posting-Host: anx_p12.wi.centuryinter.net
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4flcq9$i1k@vixen.cso.uiuc.edu>, WEIMIN YANG <w-yang3> writes:
- >I need to use a 2-D array. I use following code.
- >
- >void tryit(int a, int b) {
- >
- >float c= new float[b][a];
- >
- >....
- >
- >.....
- >
- >}
- >
- >I get compile error. Is there another way to do it?
-
- Neither C nor C++ maintain any run-time information about arrays. When you
- access an element of a 2-D array such as x[a,b] code is generated to calculate
- the address of the particular element using a formula something like:
- x + (a * max_columns + b)
- [I can't remember if C is column-major or row-major, and it's really not relevent
- here.]
- Since C does not maintain any run-time info on arrays (presumably because
- this would lead to a slower implementation of arrays), and since it does need
- max_columns to calculate element addresses, the inevitable result is that
- max_columns is a compile-time constant and that an array can only be of
- variable size in one its dimensions.
-
- >By the way, I don't want to use 1-D array to implement it.
-
- Well, you have three choices:
- 1. Use linked-lists to implement it :), or
- 2. Find an 2-D array class that someone else has already implemented, or
- 3. Pick a different programming language.
-
-